In [1]:
# variables
filename = 'CDT_2017_track.gpx'
wp_counter_limit = 50
wp_counter = 0
file_counter = 0
f = open(filename,'r')
o = open('output.gpx', 'w+')
comparison_text = '</trkpt>\n'
In [2]:
# insert EOF information for a GPX file
# create a strung variable that has everything needed and return it
# it needs to have this at the end of each file:
# </trkpt>\n</trkseg>\n</trk>\n</gpx>
def file_tags(flag):
global file_counter
if flag == 'BOF':
return '<?xml version="1.0" ?>\n<gpx creator="GaiaGPS" version="1.1" xmlns="http://www.topografix.com/GPX/1/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">\n<trk>\n<name>CDT ' + str(file_counter) + '</name>\n<desc>\n</desc>\n<trkseg>\n'
elif flag == 'NT':
if len(str(file_counter)) == 1:
return '</trkseg>\n</trk>\n<trk>\n<name>CDT 00' + str(file_counter) + '</name>\n<desc></desc>\n<trkseg>\n'
elif len(str(file_counter)) == 2:
return '</trkseg>\n</trk>\n<trk>\n<name>CDT 0' + str(file_counter) + '</name>\n<desc></desc>\n<trkseg>\n'
else:
return '</trkseg>\n</trk>\n<trk>\n<name>CDT ' + str(file_counter) + '</name>\n<desc></desc>\n<trkseg>\n'
elif flag == 'EOF':
return '</trkseg>\n</trk>\n</gpx>\n'
else:
return 'fail'
In [3]:
# method for reading the next line and compare it to some text
def line_compare(f_next_line):
if f_next_line == comparison_text:
return 'true'
elif f_next_line == '':
return 'EOF'
else:
return 'false'
In [4]:
# function to increment to a new file and close the previous file
# get file name and save it as a variable
# close the file
# increment the file counter
# open a new file
# return the new file object
def new_track(out_file):
out_file.write(file_tags('NT'))
return out_file
In [5]:
# while the waypoint counter is less than the waypoint limit, do the following
# get the next line
# write a new line to the output file
# save the previous line variable for testing at the end of the program
# compare the new line to the comparison text
# evaulate the results: true = increment wp_counter, EOL breaks out of while loop and ends program, false does nothing
# check to see if the wp_limit has been reached
# if reached, close old, open new file AND reset the wp_counter to 0
while wp_counter < wp_counter_limit:
f_string = f.readline()
o.write(f_string)
compare_result = line_compare(f_string)
if compare_result == 'true':
wp_counter += 1
elif compare_result == 'EOF':
break
if wp_counter == (wp_counter_limit-1):
file_counter += 1
o = new_track(o)
wp_counter = 0
In [6]:
o.close()
f.close()